<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php $this->need('header.php'); ?>
  <main class="row">
    <div class="container box">
      <article class="post" itemscope="" itemtype="http://schema.org/BlogPosting">
        <h1 class="post-title" itemprop="name headline"><a itemprop="url" href="<?php $this->permalink() ?>"><?php $this->title() ?></a></h1>
        <ul class="post-meta box">
          <li itemprop="author" itemscope itemtype="http://schema.org/Person"><?php _e(' '); ?><a itemprop="name" href="<?php $this->author->permalink(); ?>" rel="author"><?php $this->author(); ?></a></li>
          <li><time datetime="<?php $this->date('c'); ?>" itemprop="datePublished">发布于 <?php $this->date(); ?></time></li>
          <li><time datetime="<?php echo date('c' , $this->modified); ?>" itemprop="dateModified">最后修改于 <?php echo date('Y年m月d日' , $this->modified); ?></time></li>
          <li><?php _e(' '); ?><?php $this->category(' '); ?></li>
        </ul>
        <div class="post-content" itemprop="articleBody">
        <?php $this->content(); ?>
        </div>
        <ul class="tags box" itemprop="keywords">
            <?php
              if ($this->tags) {
                foreach ($this->tags as $tag) {
                  echo "<li><a class=\"tag\" href=\"{$tag['permalink']}\">{$tag['name']}</a></li>";
                }
              }
            ?>
        </ul>
      </article>

      <ul class="post-near">
        <li>上一篇: <?php $this->thePrev('%s','没有了'); ?></li>
        <li>下一篇: <?php $this->theNext('%s','没有了'); ?></li>
      </ul>
      <aside class="sidebar box">
        <div id="post-nav">
          <h3>段落导航</h3>
        </div>
        <div>
          <h3>同目录的文章</h3>
          <ol>
             <?php $this->widget('Widget_Archive@index', 'pageSize=9&type=category', 'slug='.$this->category)->parse('<li><a href="{permalink}">{title}</a></li>'); ?>
          </ol>
        </div>
    </aside>
    </div>

    <script>
      function addNav(html){
        var hs = html.getElementsByTagName('h3');
        if(hs.length != 0){
          var ol = document.createElement('ol');
          for(let i=0;i<hs.length;i++){
            var h = hs[i];
            h.id = `c${i}`;
            let l = document.createElement('li');
            l.innerHTML = `<a href="${h.baseURI}#c${i}">${h.innerText}</a>`;
            ol.appendChild(l);
          }
          return ol;
        }
        return;
      }

      var postNav = document.getElementById('post-nav');
      var postContent = document.getElementsByClassName('post-content')[0];
      postNav.appendChild(addNav(postContent));
    </script>

  </main>
<?php $this->need('footer.php'); ?>
